<template>
  <a-layout class="chat-container">
    <!-- 房间号展示 -->
    <a-page-header title="当前房间号" :sub-title="'#' + roomId" />

    <!-- 消息列表 -->
    <a-list class="message-list" item-layout="vertical" :data-source="messages">
      <template #renderItem="{ item }">
        <a-list-item :class="['message-item', item.isAI ? 'ai-message' : 'user-message']">
          <!-- AI消息 -->
          <div v-if="item.isAI" class="message-content">
            <a-avatar class="avatar" :src="import('@/assets/ai-avatar.jpg')" />
            <div class="bubble">{{ item.content }}</div>
          </div>

          <!-- 用户消息 -->
          <div v-else class="message-content">
            <div class="bubble">{{ item.content }}</div>
            <a-avatar class="avatar" :src="import('@/assets/user-avatar.png')" />
          </div>
        </a-list-item>
      </template>
    </a-list>

    <!-- 输入区域 -->
    <div class="input-area">
      <a-input v-model:value="inputMessage" placeholder="输入你的问题" @pressEnter="handleSend" />
      <a-button type="primary" @click="handleSend" :loading="sending"> 发送 </a-button>
    </div>
  </a-layout>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { doChatUsingPost } from '@/api/chatController'

// 随机生成6位房间号
const roomId = ref(Math.floor(100000 + Math.random() * 900000))
const messages = ref<Array<{ content: string; isAI: boolean }>>([])
const inputMessage = ref('')
const sending = ref(false)

// 初始化问候语
onMounted(() => {
  messages.value.push({
    content: '我是智逸人事的介绍员，需要我为你介绍我的项目吗',
    isAI: true,
  })
})

const handleSend = async () => {
  if (!inputMessage.value.trim()) {
    message.warning('请输入有效内容')
    return
  }

  try {
    sending.value = true

    // 添加用户消息
    messages.value.push({
      content: inputMessage.value,
      isAI: false,
    })

    // 调用接口
    const res = await doChatUsingPost({
      roomId: roomId.value,
      message: inputMessage.value,
    })

    // 添加AI回复
    messages.value.push({
      content: res.data,
      isAI: true,
    })

    inputMessage.value = ''
  } catch (e) {
    message.error('发送失败')
  } finally {
    sending.value = false
  }
}
</script>

<style scoped>
.chat-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.message-item {
  padding: 8px 0;
}

.message-content {
  display: flex;
  align-items: center;
  max-width: 80%;
}

.ai-message .message-content {
  margin-right: auto;
}

.user-message .message-content {
  margin-left: auto;
  flex-direction: row-reverse;
}

.bubble {
  margin: 0 12px;
  padding: 12px 16px;
  border-radius: 8px;
  max-width: 70%;
}

.ai-message .bubble {
  background: #f0f2f5;
}

.user-message .bubble {
  background: #1890ff;
  color: white;
}

.input-area {
  padding: 20px;
  display: flex;
  gap: 12px;
  background: #fff;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
}

.input-area :deep(.ant-input) {
  flex: 1;
}
</style>
